/**
 * Created by chenwei on 2016/5/19.
 */
angular.module('app.catagory', [
])
.config(['$stateProvider', '$urlRouterProvider', function($stateProvider, $urlRouterProvider) {
    $stateProvider
        .state('home.catagory', {
            url: '/catagory/list',
            templateUrl: "tpls/catagory/list.html",
            resolve: {
                catagory_list : ['CatagoryService', function(CatagoryService){
                    return CatagoryService.getlist().$promise;
                }]
            },
            controller: 'CatagoryListController'
        })
}])

.controller('CatagoryListController', ['$scope','catagory_list','CountyShopService', function($scope, catagory_list, CountyShopService) {
    $scope.catagory_list = catagory_list;
    $scope.submit = function () {
        //console.log($scope.shops);
        addshop($scope.shops, 0);
        function addshop(list, i) {
            if (i >= list.length) {
                addcounttownshop($scope.shops, 0);
                return;
            }
            var item = list[i];
            ShopService.addshop(item, function (data) {
                $scope.shops[i].shopid = data.shopid;
                addshop(list, i + 1);
            }, function (data) {
            });
        }

        function addcounttownshop(list, i) {
            if (i >= list.length) {
                alert("导入成功！");
                window.location.reload();
                return;
            }
            var item = list[i];
            var counttownshop = {countyid:item.countyid, townid:item.townid, shopid:item.shopid};
            CountyShopService.addcountytownshop(counttownshop, function (data) {
                addcounttownshop(list, i+1);
            }, function (data) {
                //window.location.reload();
                alert("导入失败！");
                return;
            });
        }
    }

}])
;